package cn.com.surker.weight.service.impl;

import cn.com.surker.common.Code;
import cn.com.surker.util.StringUtils;
import cn.com.surker.weight.dao.WeightMaterialContrastDao;
import cn.com.surker.weight.entity.WeightMaterialContrast;
import cn.com.surker.weight.service.WeightContrastService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author lucky
 * @create 2023年04月28 20:49
 * @desc
 */
@Slf4j
@Service
public class WeightContrastServiceImpl implements WeightContrastService {

    @Resource
    private WeightMaterialContrastDao contrastDao;

    /**
     * 分页查询未通过记录
     * @param searchVal 检索条件
     * @param offset
     * @param limit
     * @return
     */
    public IPage queryByPage(String searchVal, Long stationId, Long materialId, Long supplierId,
                      Integer offset, Integer limit) {
        if (StringUtils.isNull(offset))
            offset = 0;
        if (StringUtils.isNull(limit))
            limit = 15;
        PageHelper.offsetPage(offset, limit);
        QueryWrapper<WeightMaterialContrast> query = new QueryWrapper<>();
        if (!StringUtils.isNull(stationId))
            query.eq("station_id", stationId);
        if (!StringUtils.isNull(materialId))
            query.eq("material_id", materialId);
        if (!StringUtils.isNull(supplierId))
            query.eq("supplier_id", supplierId);
        if (!StringUtils.isNull(searchVal))
            query.and(orderWrapper -> orderWrapper.like("station", searchVal).or()
                    .like("material", searchVal).or().like("supplier", searchVal)
                    .or().like("aip_material", searchVal).or().like("remark", searchVal));

        IPage<WeightMaterialContrast> iPage = contrastDao.selectPage(new Page<>((offset + limit) /limit, limit), query);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }

    /**
     * 保存关联材料信息
     * @param stationId 站点ID
     * @param station 站点
     * @param supplierId 供应商id
     * @param supplier 供应商
     * @param materialId 材料id
     * @param material 材料
     * @param aipMaterial 供应商磅单材料
     * @param remark 备注
     * @return
     */
    public Integer save(Long stationId, String station, Integer supplierId, String supplier, Integer materialId,
                 String material, String aipMaterial, String remark) {
//        if (StringUtils.isAllParamNull(station, stationId))
//            return Code.ERROR_STATION.getCode();
        if (StringUtils.isAllParamNull(supplier, supplierId))
            return Code.ERROR_SUPPLIER.getCode();
        if (StringUtils.isAllParamNull(material, materialId))
            return Code.ERROR_MATERIAL.getCode();
        if (StringUtils.isNull(aipMaterial))
            return Code.ERROR_AIP_MATERIAL.getCode();

        QueryWrapper<WeightMaterialContrast> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id", stationId);
        queryWrapper.eq("supplier_id", supplierId);
        queryWrapper.eq("material_id", materialId);
        queryWrapper.eq("aip_material", aipMaterial);
        queryWrapper.eq("status", 0);

        List<WeightMaterialContrast> list = contrastDao.selectList(queryWrapper);
        if (list == null || list.size() == 0) {//如果记录不存在
            WeightMaterialContrast contrast = new WeightMaterialContrast(stationId, station, materialId, material,
                    supplierId, supplier, aipMaterial, 0, remark, new Date(),  new Date());
            Integer code = contrastDao.insert(contrast);
            if (code > 0)
                return 0;
            else
                return -1;
        } else {
            return Code.BIZ_DATA_EXISTS.getCode();
        }
    }

    /**
     * 修改关联材料信息
     * @param stationId 站点ID
     * @param station 站点
     * @param supplierId 供应商id
     * @param supplier 供应商
     * @param materialId 材料id
     * @param material 材料
     * @param aipMaterial 供应商磅单材料
     * @param remark 备注
     * @return
     */
    public Integer update(Long id, Long stationId, String station, Integer supplierId, String supplier, Integer materialId,
                        String material, String aipMaterial, String remark) {
        if (StringUtils.isNull(id))
            return Code.PARAM_EMPTY.getCode();

        WeightMaterialContrast contrast = contrastDao.selectById(id);
        if (contrast == null) //如果记录不存在
            return Code.ERROR_ORDER_EMPTY.getCode();
        if (!StringUtils.isNull(stationId))
            contrast.setStationId(stationId);
        if (!StringUtils.isNull(station))
            contrast.setStation(station);
        if (!StringUtils.isNull(supplierId))
            contrast.setSupplierId(supplierId);
        if (!StringUtils.isNull(supplier))
            contrast.setSupplier(supplier);
        if (!StringUtils.isNull(materialId))
            contrast.setMaterialId(materialId);
        if (!StringUtils.isNull(material))
            contrast.setMaterial(material);
        if (!StringUtils.isNull(aipMaterial))
            contrast.setAipMaterial(aipMaterial);
        if (!StringUtils.isNull(remark))
            contrast.setRemark(remark);
        Integer code = contrastDao.updateById(contrast);
        if (code > 0)
            return 0;
        else
            return -1;
    }
    /**
     * 更新状态
     * @param id
     * @param status
     * @return
     */
    public Integer updateStatus(Long id, Integer status) {
        if (StringUtils.isAllParamNull(id, status))
            return -1;
        if (status > 1 || status < 0)
            return -1;
        WeightMaterialContrast contrast = contrastDao.selectById(id);
        if (contrast == null)
            return -1;
        contrast.setStatus(status);
        Integer code = contrastDao.updateById(contrast);
        if (code > 0)
            return 0;
        else
            return -1;
    }

}
